package com.ycy.query;

import com.ycy.BaseTest;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.Process;
import org.flowable.engine.*;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.junit.Before;
import org.junit.Test;

import java.util.List;
import java.util.Map;

public class TaskQueryDemo extends BaseTest {


  //查询所有任务
  @Test
  public void query11() {
    TaskService taskService = processEngine.getTaskService();
    List<Task> tasks = taskService.createTaskQuery().list();
    System.out.println("You have " + tasks.size() + " tasks:");
    for (int i = 0; i < tasks.size(); i++) {
      Task task = tasks.get(i);
      String processInstanceId = task.getProcessInstanceId();
      System.out.println((i + 1) + ") " + task + " processInstanceId : " + processInstanceId);
      Map<String, Object> variables = taskService.getVariables(task.getId());
      System.out.println(variables);
      System.out.println(task.getProcessVariables());
      System.out.println(task.getTaskLocalVariables());
      /*
      1) Task[id=102509, name=Approve or reject request] processInstanceId : 102501
2) Task[id=105009, name=Approve or reject request] processInstanceId : 105001
3) Task[id=90009, name=Approve or reject request] processInstanceId : 90001
4) Task[id=97508, name=Holiday approved] processInstanceId : 85011
5) Task[id=97518, name=Holiday approved] processInstanceId : 87501
      * */
    }
  }


  //通过taskid 查询任务
  @Test
  public void query1111() {
    TaskService taskService = processEngine.getTaskService();
    List<Task> tasks = taskService.createTaskQuery().taskId("85019").list();
    System.out.println("You have " + tasks.size() + " tasks:");
    for (int i = 0; i < tasks.size(); i++) {
      Task task = tasks.get(i);
      System.out.println((i + 1) + ") " + task + " : " + task.getId() + " : " + task.getName());
      Map<String, Object> variables = taskService.getVariables(task.getId());
      System.out.println(variables);
    }
  }

  //通过 taskCandidateGroup 查询任务
  @Test
  public void query() {
    TaskService taskService = processEngine.getTaskService();
    List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("managers").list();
    System.out.println("You have " + tasks.size() + " tasks:");
    for (int i = 0; i < tasks.size(); i++) {
      Task task = tasks.get(i);
      System.out.println((i + 1) + ") " + task + " : " + task.getId() + " : " + task.getName());
      Map<String, Object> variables = taskService.getVariables(task.getId());
      System.out.println(variables);
    }
  }

  //查询流程下的所有任务
  @Test
  public void query1() {
    TaskService taskService = processEngine.getTaskService();
    //获取流程实例中的task
    List<Task> list = taskService.createTaskQuery().processDefinitionKey("holidayRequest").list();
    for (Task task : list) {
      System.out.println(task);
      //查询所有参数
      Map<String, Object> variables = taskService.getVariables(task.getId());
      System.out.println(variables);
    }
  }

  /**
   * 查询流程实例下的所有任务
   */
  @Test
  public void queryProcessInstanceIdTask() {
    TaskService taskService = processEngine.getTaskService();
    //获取流程实例中的task
    List<Task> list = taskService.createTaskQuery().processInstanceId("10001").list();
    for (Task task : list) {
      System.out.println(task);
      //查询所有参数
      Map<String, Object> variables = taskService.getVariables(task.getId());
      System.out.println(variables);
    }
  }

  //查询最后一个任务
  @Test
  public void test1() {
    TaskService taskService = processEngine.getTaskService();
    //获取 最后一步的 task
    List<Task> list = taskService.createTaskQuery().taskDefinitionKey("holidayApprovedTask").list();
    for (Task task : list) {
      System.out.println(task);
    }
  }

  //查询最后一个任务
  @Test
  public void test2() {
    TaskService taskService = processEngine.getTaskService();
    //获取 最后一步的 task
    List<Task> list = taskService.createTaskQuery().processDefinitionKey("living").list();
    for (Task task : list) {
      System.out.println(task);
    }
  }

  //查询最后一个任务
  @Test
  public void test22() {
    List<HistoricTaskInstance> holidayRequest = processEngine.getHistoryService()
            .createHistoricTaskInstanceQuery()
            .processDefinitionKey("living")
            .list();

    for (HistoricTaskInstance historicTaskInstance : holidayRequest) {
      System.out.println(historicTaskInstance + " " + historicTaskInstance.getEndTime());
    }
  }

  //查询最后一个任务
  @Test
  public void test233332() {
    List<HistoricTaskInstance> holidayRequest = processEngine.getHistoryService()
            .createHistoricTaskInstanceQuery()
            .processDefinitionKey("living")
            .list();

    for (HistoricTaskInstance historicTaskInstance : holidayRequest) {
      System.out.println(historicTaskInstance + " " + historicTaskInstance.getEndTime());
      BpmnModel bpmnModel = processEngine.getRepositoryService().getBpmnModel(historicTaskInstance.getProcessDefinitionId());
      Process process = bpmnModel.getMainProcess();
      FlowElement flowElement = process.getFlowElementMap().get(historicTaskInstance.getTaskDefinitionKey());
      System.out.println(flowElement);
    }

  }

  //查询历史
  @Test
  public void test3() {
    List<HistoricTaskInstance> holidayRequest = processEngine.getHistoryService()
            .createHistoricTaskInstanceQuery()
            .processDefinitionKey("holidayRequest")
//            .processInstanceBusinessKey(app.getBusinessKey())
//            .notDeleted()
            .unfinished()
            .list();

    for (HistoricTaskInstance historicProcessInstance : holidayRequest) {
      System.out.println(historicProcessInstance + " " + historicProcessInstance.getEndTime());

    }
  }

  @Test
  public void flowelement() {

    List<Task> list = processEngine.getTaskService().createTaskQuery().processInstanceId("15001").list();
    Task task = list.get(0);

    BpmnModel bpmnModel = processEngine.getRepositoryService().getBpmnModel(task.getProcessDefinitionId());
    Process process = bpmnModel.getMainProcess();
    FlowElement flowElement = process.getFlowElementMap().get("zx");
    System.out.println(flowElement);
  }

}
